package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.MktTask;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IMktTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 营销任务管理Controller
 *
 * @author xiaowai
 * @date 2023-01-18
 */
@RestController
@RequestMapping("/marketing/task")
public class MktTaskController extends BaseController
{
    @Autowired
    private IMktTaskService mktTaskService;

    /**
     * 查询营销任务管理列表
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(MktTask mktTask)
    {
        startPage();
        List<MktTask> list = mktTaskService.selectMktTaskList(mktTask);
        return getDataTable(list);
    }

    /**
     * 导出营销任务管理列表
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:export')")
    @Log(title = "营销任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MktTask mktTask)
    {
        List<MktTask> list = mktTaskService.selectMktTaskList(mktTask);
        ExcelUtil<MktTask> util = new ExcelUtil<MktTask>(MktTask.class);
        util.exportExcel(response, list, "营销任务管理数据");
    }

    /**
     * 获取营销任务管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mktTaskService.getById(id));
    }

    /**
     * 新增营销任务管理
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:add')")
    @Log(title = "营销任务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MktTask mktTask)
    {
        return toAjax(mktTaskService.save(mktTask));
    }

    /**
     * 修改营销任务管理
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:edit')")
    @Log(title = "营销任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MktTask mktTask)
    {
        return toAjax(mktTaskService.updateById(mktTask));
    }

    /**
     * 删除营销任务管理
     */
    @PreAuthorize("@ss.hasPermi('marketing:task:remove')")
    @Log(title = "营销任务管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mktTaskService.removeBatchByIds(Arrays.asList(ids)));
    }

    @GetMapping("/getTaskToDo")
    public AjaxResult getTaskToDo(Long actId){

        //根据活动的id查询出活动任务
        LoginUser loginUser = getLoginUser();

        //查询出当前活动该用户待做的任务
        List<MktTask> mktTasks = mktTaskService.getTaskToDo(actId,loginUser);
        //返回结果是返回未完成任务
        return AjaxResult.success(mktTasks);
    }

    /**
     * 完成任务的接口
     * @param taskId
     * @return
     */
    @PostMapping("/finishTask")
    public AjaxResult finishTask(Long taskId){

        //获取当前用户对象
        LoginUser loginUser = getLoginUser();

        //调用mktTaskService完成任务的方法
        mktTaskService.finishTask(taskId,loginUser);

        return AjaxResult.success("成功完成任务");

    }
}